package com.xu.modules.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xu.common.consts.XuConst;
import com.xu.common.enums.MailTypeEnum;
import com.xu.common.enums.StationNumTypeEnum;
import com.xu.common.exception.MyException;
import com.xu.common.utils.RedisCache;
import com.xu.common.utils.TokenUtil;
import com.xu.modules.entity.PersonalUserEntity;
import com.xu.modules.mapper.PersonalUserMapper;
import com.xu.modules.service.PersonalUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xu.modules.service.StationService;
import com.xu.modules.service.bo.LoginBo;
import com.xu.modules.service.bo.PersonalUserRegisterBo;
import com.xu.modules.service.vo.PersonalUserVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 个人用户 服务实现类
 * </p>
 *
 * @author xu
 * @since 2022-07-19
 */
@Service
@RequiredArgsConstructor
public class PersonalUserServiceImpl extends ServiceImpl<PersonalUserMapper, PersonalUserEntity> implements PersonalUserService {
    private final PersonalUserMapper personalUserMapper;
    private final RedisCache redisCache;
    private final TokenUtil tokenUtil;
    private final StationService stationService;

    @Override
    public List<PersonalUserVo> listAll() {
        List<PersonalUserEntity> personalUserEntityList = personalUserMapper.selectList(Wrappers.lambdaQuery());
        if(CollectionUtils.isEmpty(personalUserEntityList)){
            return new ArrayList<>();
        }
        return BeanUtil.copyToList(personalUserEntityList,PersonalUserVo.class);
    }

    @Override
    public String login(LoginBo loginBo) {
        // 验证邮箱是否正确
        validCode(loginBo.getEmail(),loginBo.getCaptchaCode(),MailTypeEnum.LOGIN);
        LambdaQueryWrapper<PersonalUserEntity> lqw = Wrappers.lambdaQuery();
        lqw.eq(PersonalUserEntity::getEmail,loginBo.getEmail());
        lqw.eq(PersonalUserEntity::getUserStatus,true);

        PersonalUserEntity personalUserEntity = personalUserMapper.selectOne(lqw);
        return tokenUtil.setToken(personalUserEntity);
    }

    @Override
    public PersonalUserVo getUserInfo() {
        return tokenUtil.getUserInfo();
    }

    @Override
    public String register(PersonalUserRegisterBo personalUserRegisterBo) {
        // 验证是否正确
        validCode(personalUserRegisterBo.getEmail(),personalUserRegisterBo.getCaptchaCode(),MailTypeEnum.REGISTER);

        PersonalUserEntity.PersonalUserEntityBuilder builder =
                PersonalUserEntity.builder()
                        .userStatus(true)
                        .email(personalUserRegisterBo.getEmail())
                        .nickname(personalUserRegisterBo.getNickname());

        // 插入数据
        PersonalUserEntity personalUserEntity = builder.build();
        personalUserMapper.insert(personalUserEntity);
        stationService.changeStationNum(StationNumTypeEnum.REGISTER);
        return tokenUtil.setToken(personalUserEntity);
    }

    /**
     * 验证邮箱
     * @param email 邮箱
     * @param code 验证码
     * @param mailTypeEnum 邮箱类型
     */
    private void validCode(String email, int code, MailTypeEnum mailTypeEnum){
        String redisKey = email + mailTypeEnum.getMailKey();
        Object obj = redisCache.getCacheObject(redisKey);
        // 过期了 或者 没有发送
        if(obj == null){
            throw new MyException("验证码无效");
        }
        // 验证码不正确
        if(Integer.parseInt(obj.toString()) != code){
            throw new MyException("验证码不正确");
        }
    }

    @Override
    public Long countByEmail(String email) {
        LambdaQueryWrapper<PersonalUserEntity> lqw = Wrappers.lambdaQuery();
        lqw.eq(PersonalUserEntity::getEmail,email);
        return personalUserMapper.selectCount(lqw);
    }
}
